package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.dto.PageDto;
import com.qf.pojo.DtsGroupon;
import com.qf.pojo.DtsGrouponRules;
import com.qf.pojo.DtsOrderGoods;
import com.qf.service.DtsGrouponRulesService;
import com.qf.service.DtsGrouponService;
import com.qf.service.DtsOrderGoodsService;
import com.qf.util.ResponseUtil;
import com.qf.vo.GrouponVo;
import com.qf.vo.RulesVo;
import com.qf.vo.SubGrouponsVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 邢式瑞
 * @version 1.0
 * @project E-commercebackGroundManagementSystem
 * @date 2022/9/25 13:39
 */

@RestController
@RequestMapping("/admin/groupon")
@Slf4j
@CrossOrigin
public class GroupOnController {
    @Autowired
    private DtsGrouponRulesService dtsGrouponRulesService;
    @Autowired
    private DtsGrouponService dtsGrouponService;
    @Autowired
    private DtsOrderGoodsService dtsOrderGoodsService;

    //http://localhost:8080/admin/groupon/list?page=1&limit=20&sort=add_time&order=desc
    @RequiresPermissionsDesc(menu = {"推广管理", "团购规则"}, button = "查找")
    @RequiresPermissions("admin:groupon:list")
    @GetMapping("/list")
    public Object grouponList(PageDto pageDto) {
        Page<DtsGrouponRules> dtsGrouponPage = dtsGrouponRulesService.selectPage(pageDto);
        long total = dtsGrouponPage.getTotal();
        List<DtsGrouponRules> records = dtsGrouponPage.getRecords();

        HashMap<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", records);
        return ResponseUtil.ok(data);
    }

    @RequiresPermissionsDesc(menu = {"推广管理", "团购规则"}, button = "编辑")
    @RequiresPermissions("admin:groupon:update")
    @PostMapping("/update")
    public Object updateTopic(@RequestBody DtsGrouponRules dtsGrouponRules) {
        if (dtsGrouponRules == null) {
            return ResponseUtil.badArgumentValue();
        }
        dtsGrouponRules.setUpdateTime(new Date());
        dtsGrouponRulesService.update(dtsGrouponRules);
        return ResponseUtil.ok(dtsGrouponRules);
    }

    @RequiresPermissionsDesc(menu = {"推广管理", "团购规则"}, button = "添加")
    @RequiresPermissions("admin:groupon:create")
    @PostMapping("/create")
    public Object createTopic(@RequestBody DtsGrouponRules dtsGrouponRules) {
        if (dtsGrouponRules == null) {
            return ResponseUtil.badArgumentValue();
        }
        dtsGrouponRules.setUpdateTime(new Date());
        dtsGrouponRules.setAddTime(new Date());
        dtsGrouponRules.setDeleted(false);
        dtsGrouponRulesService.insert(dtsGrouponRules);
        return ResponseUtil.ok(dtsGrouponRules);
    }

    @RequiresPermissionsDesc(menu = {"推广管理", "团购规则"}, button = "删除")
    @RequiresPermissions("admin:groupon:delete")
    @PostMapping("/delete")
    public Object deleteTopic(@RequestBody DtsGrouponRules dtsGrouponRules) {
        if (dtsGrouponRules == null) {
            return ResponseUtil.badArgumentValue();
        }
        dtsGrouponRules.setUpdateTime(new Date());
        dtsGrouponRules.setDeleted(true);
        dtsGrouponRulesService.delete(dtsGrouponRules);
        return ResponseUtil.ok();
    }

    //page: 1
//limit: 20
//rulesId: sss
//sort: add_time
//order: desc  admin/groupon/listRecord
    @GetMapping("/listRecord")
    public Object listRecord(PageDto pageDto) throws ParseException {
        Page<DtsGroupon> dtsGrouponIPage = dtsGrouponService.selectPage(pageDto);
        List<GrouponVo> grouponVos = new ArrayList<>();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        for (DtsGroupon dtsGroupon : dtsGrouponIPage.getRecords()) {

            List<SubGrouponsVo> subGrouponsVos = new ArrayList<>();
            GrouponVo grouponVo = new GrouponVo();

            grouponVo.setGroupon(dtsGroupon);

            SubGrouponsVo subGrouponsVo = new SubGrouponsVo();
            subGrouponsVo.setOrderId(dtsGroupon.getOrderId());
            subGrouponsVo.setUserId(dtsGroupon.getUserId());
            subGrouponsVos.add(subGrouponsVo);
            grouponVo.setSubGroupons(subGrouponsVos);


            List<DtsOrderGoods> byOrderId = dtsOrderGoodsService.selectByOrderId(dtsGroupon.getOrderId());
            for (DtsOrderGoods dtsOrderGoods : byOrderId) {
                DtsGrouponRules dtsGrouponRules = dtsGrouponRulesService.selectById(dtsOrderGoods.getGoodsId());
                if (dtsGrouponRules != null) {
                    RulesVo rulesVo = new RulesVo();
                    rulesVo.setDiscount(dtsGrouponRules.getDiscount());
                    rulesVo.setDiscountMember(dtsGrouponRules.getDiscountMember());
                    grouponVo.setRules(rulesVo);
                }
            }

            grouponVos.add(grouponVo);
        }
        Map data = new HashMap<>();
        data.put("total", dtsGrouponIPage.getTotal());
        data.put("items", grouponVos);
        return ResponseUtil.ok(data);
    }
}
